package com.LeeCode;

import java.util.HashSet;

/**
 * 链表组件
 */

public class Code817 {
    public static void main(String[] args) {
        int[] arr = {0, 1, 2, 3, 4};
        int[] nums = {0, 3, 1, 4};
        ListNode head = Utils.arrayToListNode(arr);
        System.out.println(new Code817().numComponents(head, nums));
    }

    public int numComponents(ListNode head, int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int ans = 0, count = 0;
        while (head != null) {
            if (set.contains(head.val)) {
                if (count == 0) ans++;
                count++;
            } else {
                count = 0;
            }
            head = head.next;
        }
        return ans;
    }
}
